package P3;

import java.io.*;
import java.net.*;


/**
 * 基于线程的Echo客户端/服务器程序设计
 * 3.1基于线程的Echo客户端/服务器程序设计
 * 3.1.4客户端程序：
 */
public class EchoThreadClient {
    private static final String SERVER_HOST = "localhost";
    private static final int SERVER_PORT = 8888;

    public static void main(String[] args) {
        try (Socket socket = new Socket(SERVER_HOST, SERVER_PORT);
             BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
             PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader serverReader = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

            System.out.println("Connected to server. Type a message and press Enter to send.");

            String userInput;
            while ((userInput = reader.readLine()) != null) {
                writer.println(userInput);
                String serverResponse = serverReader.readLine();
                System.out.println("Server response: " + serverResponse);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}